package com.itzb.asone.application;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.itzb.asone.constant.SystemConstant;
import com.itzb.asone.constant.SystemParamConstant;
import com.itzb.asone.constant.model.SysResourceConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysGroup;
import com.itzb.asone.system.model.SysResource;
import com.itzb.asone.system.model.SysUser;
import com.itzb.asone.system.service.ISysGroupService;
import com.itzb.asone.system.service.ISysParamService;
import com.itzb.asone.system.service.ISysResourceService;
import com.itzb.asone.system.service.ISysUserService;

/**
 * 获取用户菜单信息
 *
 */
public class AppContext {

	private static ISysUserService sysUserService;
	private static ISysResourceService sourceService;
	private static ISysGroupService sysGroupService;
	private static ISysParamService sysParamService;

	@Autowired
	public void setSysUserService(ISysUserService sysUserService) {
		AppContext.sysUserService = sysUserService;
	}
	@Autowired
	public void setSourceService(ISysResourceService sourceService) {
		AppContext.sourceService = sourceService;
	}
	@Autowired
	public void setSysGroupService(ISysGroupService sysGroupService) {
		AppContext.sysGroupService = sysGroupService;
	}
	@Autowired
	public void setSysParamService(ISysParamService sysParamService) {
		AppContext.sysParamService = sysParamService;
	}
	
	
	
	
	
	public static SysUser getUserByUserId(String userId){
		SysUser user = sysUserService.queryEntityById(userId);
		return user;
	}
	
	public static SysGroup getGroupByUserId(String userId){
		SysGroup sysGroup = sysGroupService.getGroupByUserId(userId);
		return sysGroup;
	}
	

	public static ApplicationResourceInfo getUserResource(SysUser sysUser,String url,HttpServletRequest httpRequest){
		
		List<SysResource> list = SourceList(sysUser);
		ApplicationResourceInfo ari = new ApplicationResourceInfo(sysUser.getId(),null,list,null);

		return ari;
	}

	
	public static boolean getUserResourceAuth(SysUser sysUser,String url){
		Criteria<SysResource> param = new Criteria<SysResource>();
		param.addParam("userId", sysUser.getId());
		param.addParam("url", url);
		param.addParam("accessAuth", SysResourceConstant.T_SYS_RESOURCE_ACCESS_AUTH_2);
		List<SysResource> allSysResource = sourceService.getResource(param);
		if(allSysResource == null || allSysResource.size() <=0){
			return false;
		} else {
			return true;
		}
	}


	public static boolean getUserResourceObj(SysUser sysUser,String url){
		Criteria<SysResource> param = new Criteria<SysResource>();
		param.addParam("url", url);
		List<SysResource> allSysResource = sourceService.queryPage(param);
		if(allSysResource == null || allSysResource.size() <=0){
			return false;
		} else {
			return true;
		}
	}
	
	
	private static List<SysResource> SourceList(SysUser user){
		List<SysResource> sysResourceList = new ArrayList<SysResource>();

		Criteria<SysResource> param = new Criteria<SysResource>();
		param.addParam("notResourceType",SysResourceConstant.T_SYS_RESOURCE_RESOURCE_TYPE_2);
		param.addParam("parentResourceId",SystemConstant.TOP_MENU_CODE);
		param.addParam("userId", user.getId());
		param.addParam("accessAuth", SysResourceConstant.T_SYS_RESOURCE_ACCESS_AUTH_2);
		List<SysResource> allSysResource = sourceService.getResource(param);
		for(SysResource rootNode : allSysResource) {
			SysResource node = getSysResource(user,rootNode);
			sysResourceList.add(node);
		}
		return sysResourceList;

	}

	public static SysResource getSysResource(SysUser user,SysResource sysResources ){
		List<SysResource> resourceChildList = new ArrayList<SysResource>();
		Criteria<SysResource> param = new Criteria<SysResource>();
		param.addParam("notResourceType",SysResourceConstant.T_SYS_RESOURCE_RESOURCE_TYPE_2);
		param.addParam("parentResourceId", sysResources.getResourceId());
		param.addParam("userId", user.getId());
		param.addParam("accessAuth", SysResourceConstant.T_SYS_RESOURCE_ACCESS_AUTH_2);
		List<SysResource> temp = sourceService.getResource(param);

		if (temp.size() > 0) {
			for (SysResource resourcesChild : temp) {
				resourceChildList.add(resourcesChild);
				sysResources.setResourceChild(resourceChildList);
				getSysResource(user,resourcesChild);
			}
		}
		return sysResources;
	}
	
	public static List<Map<String, String>> getBreadcrumb(String userId,String hradurl,String url) {
		Criteria<SysResource> param = new Criteria<SysResource>();
		String resourceId = "";
		param.addParam("url", url);
		List<SysResource> sysResourceList = sourceService.getResource(param);
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		SysResource sysResource = new SysResource();
		Map<String, String> map = new HashMap<String, String>();
		while(true){
			if(sysResourceList != null && sysResourceList.size() >0){
				sysResource = sysResourceList.get(0);
				map = new HashMap<String, String>();
				map.put("url", url);
				map.put("resourceName", sysResource.getResourceName());
				map.put("iconClass", sysResource.getIconClass());
				list.add(map);
			} else {
				break;
			}
			if(sysResource.getParentResourceId() != null && !sysResource.getParentResourceId().equals(SystemConstant.TOP_MENU_CODE)){
				param.clearParams();
				param.addParam("resourceId", sysResource.getParentResourceId());
				sysResource = sourceService.queryPage(param).get(0);
				resourceId = sysResource.getParentResourceId();
				url = sysResource.getUrl();
				map = new HashMap<String, String>();
				map.put("url", url);
				map.put("resourceName", sysResource.getResourceName());
				map.put("iconClass", sysResource.getIconClass());
				list.add(map);
				if(resourceId == null || resourceId.equals(SystemConstant.TOP_MENU_CODE)){
					break;
				} else {
					if(sysResource.getParentResourceId() != null && !sysResource.getParentResourceId().equals(SystemConstant.TOP_MENU_CODE)){
						param.clearParams();
						param.addParam("resourceId", sysResource.getParentResourceId());
						sysResource = sourceService.queryPage(param).get(0);
						resourceId = sysResource.getParentResourceId();
						url = sysResource.getUrl();
						map = new HashMap<String, String>();
						map.put("url", url);
						map.put("resourceName", sysResource.getResourceName());
						map.put("iconClass", sysResource.getIconClass());
						list.add(map);
						if(resourceId == null || resourceId.equals(SystemConstant.TOP_MENU_CODE)){
							break;
						} else {
							break;
						}
					}
				}
			} else {
				break;
			}
		}
		//首页
		map = new HashMap<String, String>();
		map.put("url", hradurl + "/system/main");
		map.put("resourceName", "Home");
		map.put("iconClass", "fa-home");
		list.add(map);
		List<Map<String, String>> result = new ArrayList<Map<String,String>>();
		int j = list.size();
		for(int i=0;i<j;i++){
			result.add(list.get(j-i-1));
		}
		return result;
	}
	
	public static String getPageTitle(){
		return sysParamService.getParamValue(SystemParamConstant.PAGE_TITLE);
	}
}
